# -*- coding:utf-8 -*-
"""
过滤器
"""
__author__ = "shu2015626"

from django.db.models import Q
from django_filters import rest_framework as filters

from .models import Goods


class GoodsFilter(filters.FilterSet):
    """
    商品的过滤类
    """
    pricemin = filters.NumberFilter(field_name="shop_price", lookup_expr="gte", help_text="最低价格")
    pricemax = filters.NumberFilter(field_name="shop_price", lookup_expr="lte", help_text="最高价格")
    # name = filters.CharFilter(field_name="name", lookup_expr="icontains")
    top_category = filters.NumberFilter(method='top_category_filter', help_text="商品类别id")

    def top_category_filter(self, queryset, name, value):
        return queryset.filter(Q(category_id=value) | Q(category__parent_category_id=value) | Q(
            category__parent_category__parent_category_id=value))

    class Meta:
        model = Goods
        # fields = ['pricemin', 'pricemax', 'name']
        fields = ['pricemin', 'pricemax', 'is_hot', 'is_new']